﻿using System.Collections.Generic;
using BenchMark.ShootingGame;
using FarseerPhysics;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syngine;
using Syngine.Components.Screens;
using Syngine.Physics;

namespace BenchMark.PlatformerGame
{
	public class PlatformerScreen : Screen
	{
		private BlueBox _greenbox;
		private BlueBox _bluebox;
		private BlueBox _redbox;
		private BlueBox _brownbox;
		private Player _player;

		private readonly List<BlueBox> _boxes = new List<BlueBox>();

		public PlatformerScreen(ScreenController screenController) : base(screenController)
		{
		}

		public override void Initialize()
		{
			_player = new Player();
			_player.Initialize();
			_greenbox = new BlueBox(Color.Green);
			_greenbox.Initialize();
			_bluebox = new BlueBox(Color.Blue);
			_bluebox.Initialize();
			_redbox = new BlueBox(Color.Red);
			_redbox.Initialize();
			_brownbox = new BlueBox(Color.Brown);
			_brownbox.Initialize();

			base.Initialize();
		}

		public override void LoadContent(LoadContext context)
		{
			var bounds = BodyFactory.CreateLoopShape(WorldContext.World, GetWorldBounds(context.GraphicsDevice), 1f);
			bounds.CollidesWith = Category.All;

			_player.LoadContent(context);
			_player.Position = new Vector2(734);

			_greenbox.LoadContent(context);
			_greenbox.Position = new Vector2(750);
			_greenbox.Body.Restitution = 0.0f;
			_greenbox.Body.BodyType = BodyType.Static;

			_redbox.LoadContent(context);
			_redbox.Position = new Vector2(850);
			_redbox.Body.Restitution = 0.0f;
			_redbox.Body.BodyType = BodyType.Static;

			_bluebox.LoadContent(context);
			_bluebox.Position = new Vector2(950);
			_bluebox.Body.Restitution = 0.0f;
			_bluebox.Body.BodyType = BodyType.Static;

			_brownbox.LoadContent(context);
			_brownbox.Position = new Vector2(1050);
			_brownbox.Body.Restitution = 0.0f;
			_brownbox.Body.BodyType = BodyType.Static;

			//var box = new BlueBox(context.GraphicsDevice.Viewport.Width, 2f);
			//box.Initialize();
			//box.LoadContent(context);
			//box.Position = new Vector2(0, 950);
			//box.Body.Restitution = 0.0f;
			//box.Body.BodyType = BodyType.Static;
			//_boxes.Add(box);

			//for (int i = 0; i < 100; i++)
			//{
			//	box = new BlueBox(10, 10);
			//	box.Initialize();
			//	box.LoadContent(context);
			//	box.Position = new Vector2(i * 70, 975);
			//	box.Body.Restitution = 0.0f;
			//	box.Body.BodyType = BodyType.Static;
			//	_boxes.Add(box);
			//}

			base.LoadContent(context);
		}

		protected override void UpdateCore(UpdateContext context)
		{
			_player.Update(context);
			_greenbox.Update(context);
			_bluebox.Update(context);
			_redbox.Update(context);
			_brownbox.Update(context);

			for (int i = 0; i < _boxes.Count; i++)
			{
				_boxes[i].Update(context);
			}
		}

		public override void UnloadContent()
		{
			
		}

		protected override void DrawCore(DrawContext context)
		{
			_player.Draw(context);
			_greenbox.Draw(context);
			_bluebox.Draw(context);
			_redbox.Draw(context);
			_brownbox.Draw(context);

			for (int i = 0; i < _boxes.Count; i++)
			{
				_boxes[i].Draw(context);
			}
		}

		private static Vertices GetWorldBounds(GraphicsDevice graphics)
		{
			var width = ConvertUnits.ToSimUnits(10000);
			var height = ConvertUnits.ToSimUnits(graphics.Viewport.Height);

			return new Vertices(new[]
			{
				new Vector2(0, 0),
				new Vector2(width, 0),
				new Vector2(width, height),
				new Vector2(0, height)
			});
		}
	}
}